<?php

/**
 * Created by PhpStorm.
 * User: dumbledore
 * Date: 2017/12/21
 * Time: 下午5:09
 */
class Courier_model extends CI_Model
{

    public function __construct(){
        $this->load->database();
        $this->load->helper("common");
    }

    // 查询单子当前的状态
    function get_app_user_box($id){
        $sql = "SELECT * FROM app_user_box WHERE id = $id;";
        return $this->db->query($sql)->row_array();
    }

    // 查询当前运输箱子的快递信息
    function get_user_box_info($box_id){
        $sql = "SELECT * FROM app_user_box WHERE id = $box_id;";
        return $this->db->query($sql)->row_array();
    }

    // 查询箱子最新在谁那儿
    function query_box_status($box_id)
    {
        $sql = "select * from app_courier_box WHERE box_id = $box_id AND status = 1 ORDER BY created_at DESC ";
        return $this->db->query($sql)->row_array();
    }

    // 对app_courier_box状态做变更
    function update_app_courier_box($status,$box_id,$courier_id)
    {
        $sql = "SELECT * FROM app_courier_box WHERE box_id = $box_id AND courier_id = $courier_id;";
        $info = $this->db->query($sql)->row_array();
        if($info){
            // 只做更新
            $bool = $this->db->where(array("courier_id"=>$courier_id,"box_id"=>$box_id))->update('app_courier_box',array("status"=>$status,"created_at"=>date("Y-m-d H:i:s")));
        }else{
            // 做插入
            $data_arr['box_id'] = $box_id;
            $data_arr['courier_id'] = $courier_id;
            $data_arr['status'] = $status;
            $data_arr['created_at'] = date("Y-m-d H:i:s");
            $bool = $this->db->insert("app_courier_box",$data_arr);
        }
        return $bool;

    }

    // 查询快递员信息
    function query_courier_info($id){
        $sql = "SELECT verify_status, verify_reason, wechat_id, real_name,tel,company,code,bn,id_number,front_pic,back_pic,remark FROM app_courier WHERE id = $id;";
        $r1 = $this->db->query($sql)->row_array();
        $wechat_id = $r1['wechat_id'];
        $r1['front_pic'] = $this->config->item("upload_url").$r1['front_pic'];
        $r1['back_pic'] = $this->config->item("upload_url").$r1['back_pic'];

        $sql2 = "select nick_name,avatar_url from app_wechat WHERE id =$wechat_id";
        $r2 = $this->db->query($sql2)->row_array();
        return array_merge($r1,$r2);
    }

}